package com.fingard.dsp.bank.sepro;

import com.fingard.FGBiz;
import com.fingard.client.DspClient;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.bank.helpers.FileType;
import com.fingard.dsp.bank.helpers.HelperResp;
import com.fingard.dsp.bank.syncdata.SaveToDb;
import com.fingard.dsp.tcpserver.RequestNIO;
import com.fingard.generalobj.ExecResponse;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.text.XmlHelper;

/**保存其它DSP发送至本DSP的文件和数据*/
public class ReceiptSaveToAndTransmit {
	
	public boolean isTransmit;
	public String transmitDest;
	private int timeoutSec;
	
	public ReceiptSaveToAndTransmit() throws Exception{
		isTransmit = false;
		transmitDest = "";
		timeoutSec = 30;
		init();
	}
	
	private void init() throws Exception{
		ReceiptProConfig proConfig = ReceiptPro.getReceiptProConfig();
		if(proConfig.isTransmitReceiptReq){
			isTransmit = true;
			transmitDest = proConfig.transmitDestServer;
			timeoutSec = proConfig.getValueAsInt("timeoutSec", 30, "transmit");
		}/*else{
			FileSaveSet fileSet = FGBiz.dspCenter.dspConfig.getFileSaveSet("receipt");
			if(fileSet !=null && fileSet.isTransmitToMainHost){
				isTransmit = true;
				transmitDest = fileSet.mainHostServer;
				StringBuilder sbLog = new StringBuilder();
				timeoutSec = fileSet.getReadTimeoutSec(15, sbLog);
				if(sbLog.length() > 0){
					FGBiz.limitMsg.error("ReceiptTransmit", "fileSet.getReadTimeoutSec："+sbLog.toString());
				}
			}
		}*/
	}

	/**转发回单文件*/
	public ExecResponse transmitSyncFile(RequestNIO pReqNio){
		ExecResponse resp = new ExecResponse();
		String logCode = "ReceiptTransmit.transmitSyncFile";
		if(isTransmit){
			if(StringHelper.hasAnyChar(transmitDest)){
				//sbLog.append("回单文件转发至："+transmitDest+"\r\n");
				DspClient dspClient = new DspClient(transmitDest);
				dspClient.timeoutSec = timeoutSec;
				String retXml = "";
				try {
					TcpReceiver retReceiver = dspClient.syncFile(pReqNio.reqSocketReceiver.trgFilePath, FileType.receipt, pReqNio.bankHeader.bankCode, pReqNio.bankHeader.syBank, pReqNio.reqSocketReceiver.oriFilePath);
					if(retReceiver.isCompleted){
						resp.isSucceed = true;
	    				retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex,retReceiver.contentLength, retReceiver.charsetName);
	    			}
	    			else{
	    				retXml = "推送"+transmitDest+"失败："+retReceiver.getConnMsg()+"，文件"+pReqNio.reqSocketReceiver.trgFilePath;
	    				FGBiz.limitMsg.error(logCode, retXml);
	    				if(retReceiver.connState.equals(ConnState.FAIL)){
	    					resp.logMsg = "回单文件转发，连接主DSP服务"+transmitDest+"失败";
	    					FGBiz.limitMsg.error(logCode, resp.logMsg);
	    				}
	    			}
					FileHelper.deleteFileIfExists(pReqNio.reqSocketReceiver.trgFilePath);
				} catch (Exception e) {
					resp.logMsg = "回单文件转发至："+transmitDest +"异常：" + LogHelper.getStackTrace(e)+"\r\nretXml:"+retXml;
					FGBiz.limitMsg.exception(logCode, e, "回单文件转发至："+transmitDest+"，响应："+retXml);
				}
				resp.setResult(retXml);
			}else{
				resp.logMsg = "转发目的地址transmitDestServer配置为空";
				FGBiz.limitMsg.error(logCode, resp.logMsg);
			}
		}else{
			resp.isSucceed = true;
		}
		return resp;
	}
	
	/**保存或转发回单数据*/
	public ExecResponse saveToOrTransmitDb(RequestNIO pReqNio){
		ExecResponse resp = new ExecResponse();
		String logCode = "ReceiptTransmit.transmitSaveToDb";
		if(isTransmit){
			if(StringHelper.hasAnyChar(transmitDest)){
				DspClient dspClient = new DspClient(transmitDest);
				dspClient.timeoutSec = timeoutSec;
				String retXml = "";
				try {
					TcpReceiver retReceiver = dspClient.saveToDb(pReqNio.sbReq.toString());
					if(retReceiver.isCompleted){
						resp.isSucceed = true;
	    				retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex,retReceiver.contentLength, retReceiver.charsetName);
	    			}else{
	    				retXml = "推送"+transmitDest+"失败："+retReceiver.getConnMsg()+"，回单数据："+pReqNio.sbReq.toString();
	    				FGBiz.limitMsg.error(logCode, retXml);
	    				if(retReceiver.connState.equals(ConnState.FAIL)){
	    					resp.logMsg = "回单数据转发，连接主DSP服务"+transmitDest+"失败";
	    					FGBiz.limitMsg.error(logCode, resp.logMsg);
	    				}
	    			}
				} catch (Exception e) {
					resp.logMsg = "回单数据转发至："+transmitDest +"异常：" + LogHelper.getStackTrace(e)+"\r\nretXml:"+retXml;
					FGBiz.limitMsg.exception(logCode, e, "回单数据转发至："+transmitDest+"，响应："+retXml);
				}
				resp.setResult(retXml);
			}else{
				resp.logMsg = "转发目的地址transmitDestServer配置为空";
				FGBiz.limitMsg.error(logCode, resp.logMsg);
			}
		}else{
			SaveToDb saveHelper = new SaveToDb();
			HelperResp svResp = saveHelper.doSave(pReqNio.sbReq.toString());
			String tmpRespCode = "-1";
			String tmpRespInfo = "";
			if(svResp.isCompleted){
				tmpRespCode="0";
				tmpRespInfo="保存成功";
				resp.isSucceed = true;
			}else{
				tmpRespInfo=svResp.sbMsg.toString();
			}
			resp.setResult("<ATSYH><TransResp><TransParam><RespCode>"+tmpRespCode+"</RespCode><RespInfo>"+XmlHelper.encode(tmpRespInfo)+"</RespInfo></TransParam></TransResp></ATSYH>");
		}
		return resp;
	}
	
}
